const { add, update, remove, getCount, query } = require('../../utils/dbTools');
const { getUploadedUrl } = require('../../utils/upload');
const { isValidator, createLog } = require('../../utils/util');
const md5 = require('md5');
// 数据表名称
const tableName = 'admin';
const roleTableName = 'role';


// 请求数据总条数
const adminTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const result = await getCount(tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}

/**
 * 查询管理员列表
 */
const adminList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query;
    if(!page||!size){
        output.msg="参数错误:缺少关键参数page或者size"
        return res.send(output)
    }
    let sql = `select a.id,a.username,a.phone,a.avatar,a.phone,a.email,a.realname,a.nickname,a.status,a.create_time,r.id as rid, r.rolename from ${tableName} a `;
    sql += ` inner join ${roleTableName} r on a.rid=r.id order by a.id asc limit ${(page - 1) * size},${size}`;
    const result = await query(sql);
    output.data = result;
    res.send(output);
}

/**
 * 新增管理员
 * 
 */
const adminAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { rid, username, password, phone, realname, nickname, email } = req.body;

    // 检测用户名是否可用
    const valid = await isValidator(tableName, 'username', username);
    if (!valid) {
        output.code = 500;
        output.msg = '数据已存在,不能重复添加';
        return res.send(output);
    }

    const { file } = req;
    if (!rid || !username || !password || !phone) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // 对密码进行md5加密处理
    password = md5(password.toString());
    // 要新增的数据
    // 注册时间
    const create_time = Date.now();
    const data = { rid, username, password, phone, create_time };

    if (realname) data.realname = realname;
    if (nickname) data.nickname = nickname;
    if (email) data.email = email;

    if (file) {
        // 获取图片上传地址
        data.avatar = getUploadedUrl(file);
    }
    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '添加管理员失败';
        return res.send(output);
    }
    output.data = result;
    createLog(req, { type: 12, remark: "新增管理员账号" });

    res.send(output);
}

/**
 * 更新管理员信息
*/
const adminUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { id, rid, password, phone, email, realname, nickname, status } = req.body;

    const { file } = req;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // 要更新的数据
    const data = {};

    if (rid) data.rid = rid;
    if (phone) data.phone = phone;
    if (email) data.email = email;
    if (status) data.status = status;
    if (realname) data.realname = realname;
    if (nickname) data.nickname = nickname;

    // 对密码进行MD5加密处理
    if (password) data.password = md5(password);
    // 获取上传图片的地址
    if (file) data.avatar = getUploadedUrl(file);
    const result = await update(id, data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '更新管理员信息失败';
        return res.send(output);
    }
    createLog(req, { type: 13, remark: "更新管理员账号" });
    res.send(output);
}
/**
 * 删除管理员
*/

const adminRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    const result = await remove(id, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "删除管理员失败";
        return res.send(output);
    }
    createLog(req, { type: 5, remark: "删除管理员账号" });

    res.send(output);
}



module.exports = {
    adminList,
    adminAdd,
    adminUpdate,
    adminRemove,
    adminTotal
}